{include file='Public/header' /}
<div class="layui-layout layui-layout-admin">
{include file='Public/head' /}	
{include file='Public/side' /}
<div class="layui-body"> 
{include file='Public/crumbs' /}
<style>
.hide{ display:none;}
</style>
	<!-- 内容主体区域 -->
	<div class="shu-body">
		<div class="layui-fluid">
			<div class="layui-row layui-col-space15">
				<div class="layui-col-md12 layui-col-xs12">
					<form class="layui-form" method="post">
						<a href="{:url('add')}" class="layui-btn"><i class="layui-icon">&#xe61f;</i> 添加用户组</a>
						<table lay-even class="layui-table">
							<colgroup>
							<col width="50">
							</colgroup>
							<thead>
								<tr>
									<th> 
									<input type="checkbox" name="checkAll" lay-filter="checkAll">
									</th>
									<th>用户组</th>
                                    <th>状态</th>
                                    <th class="center">操作</th>
								</tr>
							</thead>
							<tbody>
							{volist name="list" id="val"}
							<tr>
								<td align="center"> 
								{if condition="$val['id'] neq 1"}
								<input type="checkbox" name="ids[{$val['id']}]" lay-filter="checkOne" value="{$val['id']}">
                                {else/}
                                <span title="系统用户组，禁止删除">--</span>
								{/if}
								</td>
								<td>{$val['title']}</td>
								<td>
								{if condition="$val['id'] eq 1 "} 
								<input type="checkbox" disabled name="switch" lay-skin="switch" lay-text="禁|禁">
								{elseif  condition="$val['status'] eq 1" /}
								<input type="checkbox" checked name="switch" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switchTest" value="1" val='{:url('status',array('id'=>$val['id']))}'> 
								{else /} 
								<input type="checkbox" name="switch" lay-skin="switch" lay-text="显示|隐藏" lay-filter="switchTest" value="1" val='{:url('status',array('id'=>$val['id']))}'>
								{/if}
                                </td>
								<td>
								<a class="layui-btn layui-btn-small" href="{:url('edit',array('id'=>$val['id']))}">修改</a>
								</td>
							</tr>
							{/volist}
							</tbody>
						</table>
						<a class="layui-btn" lay-submit lay-filter="delete"><i class="layui-icon">&#xe640;</i> 批量删除</a>
					</form>
				</div>
			</div>
		</div>
	</div>
	<!-- 内容主体区域 END--> 
</div>
{include file='Public/footer' /} 
<script>
layui.use('form',function(){
  var form = layui.form,
  jq = layui.$;

  form.on('checkbox(checkAll)', function(data){
    if(data.elem.checked){
      jq("input[lay-filter='checkOne']").prop('checked',true);
    }else{
      jq("input[lay-filter='checkOne']").prop('checked',false);
    }
    form.render('checkbox');
  });  

  form.on('checkbox(checkOne)', function(data){
    var is_check = true;
    if(data.elem.checked){
      jq("input[lay-filter='checkOne']").each(function(){
        if(!jq(this).prop('checked')){ is_check = false; }
      });
      if(is_check){
        jq("input[lay-filter='checkAll']").prop('checked',true);
      }
    }else{
      jq("input[lay-filter='checkAll']").prop('checked',false);
    } 
    form.render('checkbox');
  });
  
  //监听指定开关 
  form.on('switch(switchTest)', function(data){
	  var url = jq(this).attr('val');
	  loading = layer.load(2, {
		shade: [0.2,'#000']
	  });
      jq.get(url,function(data){
        if(data.code == 200){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            //location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
        }
      });
  });
  
  

  form.on('submit(delete)', function(data){
    var is_check = false;
    jq("input[lay-filter='checkOne']").each(function(){
      if(jq(this).prop('checked')){ is_check = true; }
    });
    if(!is_check){
      layer.msg('请选择数据', {icon: 2,anim: 6,time: 1000});
      return false;
    }
    layer.confirm('确定批量删除?', function(index){
      loading = layer.load(2, {
        shade: [0.2,'#000']
      });
      var param = data.field;
      jq.post("{:url('del')}",param,function(data){
        if(data.code == 200){
          layer.close(loading);
          layer.msg(data.msg, {icon: 1, time: 1000}, function(){
            location.reload();
          });
        }else if(data.code == 0){
          layer.close(loading);
          layer.msg(data.msg, {icon: 2,anim: 6, time: 1000});
        }
      });
    });
	
    return false;
  });
  
 
})
</script> 
